﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;

namespace SVAP
{
    public partial class RecentVideoForm : Form
    {
        private static string strExePath = "";// ".exe"文件所在路径
        private int count;  //计数器
        private int temp;   //计时

        public RecentVideoForm()
        {
            InitializeComponent();

            //将该窗口设置为程序的焦点，且不允许改变，直至关闭该窗口
            //SetWindow setWin = new SetWindow(this.Handle);
            //setWin.Star();

            //底层控件不可见
            pictureBox_TopLeft.BackColor = Color.White;
            pictureBox_TopMiddle.BackColor = Color.White;
            pictureBox_TopRight.BackColor = Color.White;
            pictureBox_BaseLeft.BackColor = Color.White;
            pictureBox_BaseMiddle.BackColor = Color.White;
            pictureBox_BaseRight.BackColor = Color.White;

            //视频播放器格式
            axWMP_TopLeft.uiMode = "none";
            axWMP_TopMiddle.uiMode = "none";
            axWMP_TopRight.uiMode = "none";
            axWMP_BaseLeft.uiMode = "none";
            axWMP_BaseMiddle.uiMode = "none";
            axWMP_BaseRight.uiMode = "none";

            temp = 0;
            timer_control.Enabled = true;
        }

    #region  显示效果控制

        //当鼠标移动到Label_TopLeft上时，字体显示为红色
        private void label_TopLeft_MouseEnter(object sender, EventArgs e)
        {
            label_TopLeft.ForeColor = Color.Red; 
        }

        //当鼠标移出Label_TopLeft上时，字体恢复为黑色
        private void label_TopLeft_MouseLeave(object sender, EventArgs e)
        {
            label_TopLeft.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_TopMiddle上时，字体显示为红色
        private void label_TopMiddle_MouseEnter(object sender, EventArgs e)
        {
            label_TopMiddle.ForeColor = Color.Red;
        }

        //当鼠标移出Label_TopMiddle上时，字体恢复为黑色
        private void label_TopMiddle_MouseLeave(object sender, EventArgs e)
        {
            label_TopMiddle.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_TopRight上时，字体显示为红色
        private void label_TopRight_MouseEnter(object sender, EventArgs e)
        {
            label_TopRight.ForeColor = Color.Red;
        }

        //当鼠标移出Label_TopRight上时，字体恢复为黑色
        private void label_TopRight_MouseLeave(object sender, EventArgs e)
        {
            label_TopRight.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_BaseLeft上时，字体显示为红色
        private void label_BaseLeft_MouseEnter(object sender, EventArgs e)
        {
            label_BaseLeft.ForeColor = Color.Red;
        }

        //当鼠标移出Label_BaseLeft上时，字体恢复为黑色
        private void label_BaseLeft_MouseLeave(object sender, EventArgs e)
        {
            label_BaseLeft.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_BaseMiddle上时，字体显示为红色
        private void label_BaseMiddle_MouseEnter(object sender, EventArgs e)
        {
            label_BaseMiddle.ForeColor = Color.Red;
        }

        //当鼠标移出Label_BaseMiddle上时，字体恢复为黑色
        private void label_BaseMiddle_MouseLeave(object sender, EventArgs e)
        {
            label_BaseMiddle.ForeColor = Color.Black;
        }

        //当鼠标移动到Label_BaseRight上时，字体显示为红色
        private void label_BaseRight_MouseEnter(object sender, EventArgs e)
        {
            label_BaseRight.ForeColor = Color.Red;
        }

        //当鼠标移出Label_BaseRight上时，字体恢复为黑色
        private void label_BaseRight_MouseLeave(object sender, EventArgs e)
        {
            label_BaseRight.ForeColor = Color.Black;
        }

        //当鼠标移动过TopLeft视频控件时，显示该控件的边缘
        private void pictureBox_TopLeft_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_TopLeft.BackColor = Color.Red;
        }

        //当鼠标离开TopLeft视频控件时，不显示该控件的边缘
        private void pictureBox_TopLeft_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_TopLeft.BackColor = Color.White;
        }

        //当鼠标移动过TopMiddle视频控件时，显示该控件的边缘
        private void pictureBox_TopMiddle_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_TopMiddle.BackColor = Color.Red;
        }

        //当鼠标离开TopMiddle视频控件时，不显示该控件的边缘
        private void pictureBox_TopMiddle_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_TopMiddle.BackColor = Color.White;
        }

        //当鼠标移动过TopRight视频控件时，显示该控件的边缘
        private void pictureBox_TopRight_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_TopRight.BackColor = Color.Red;
        }

        //当鼠标离开TopRight视频控件时，不显示该控件的边缘
        private void pictureBox_TopRight_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_TopRight.BackColor = Color.White;
        }

        //当鼠标移动过TopLeft视频控件时，显示该控件的边缘
        private void pictureBox_BaseLeft_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_BaseLeft.BackColor = Color.Red;
        }

        //当鼠标离开TopLeft视频控件时，不显示该控件的边缘
        private void pictureBox_BaseLeft_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_BaseLeft.BackColor = Color.White;
        }

        //当鼠标移动过TopLeft视频控件时，显示该控件的边缘
        private void pictureBox_BaseMiddle_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_BaseMiddle.BackColor = Color.Red;
        }

        //当鼠标离开TopLeft视频控件时，不显示该控件的边缘
        private void pictureBox_BaseMiddle_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_BaseMiddle.BackColor = Color.White;
        }

        //当鼠标移动过TopLeft视频控件时，显示该控件的边缘
        private void pictureBox_BaseRight_MouseEnter(object sender, EventArgs e)
        {
            pictureBox_BaseRight.BackColor = Color.Red;
        }

        //当鼠标离开TopLeft视频控件时，不显示该控件的边缘
        private void pictureBox_BaseRight_MouseLeave(object sender, EventArgs e)
        {
            pictureBox_BaseRight.BackColor = Color.White;
        }

        private void axWMP_TopLeft_MouseMoveEvent(object sender, AxWMPLib._WMPOCXEvents_MouseMoveEvent e)
        {
            pictureBox_TopLeft.BackColor = Color.Red;
        }

        private void axWMP_TopMiddle_MouseMoveEvent(object sender, AxWMPLib._WMPOCXEvents_MouseMoveEvent e)
        {
            pictureBox_TopMiddle.BackColor = Color.Red;
        }

        private void axWMP_TopRight_MouseMoveEvent(object sender, AxWMPLib._WMPOCXEvents_MouseMoveEvent e)
        {
            pictureBox_TopRight.BackColor = Color.Red;
        }

        private void axWMP_BaseLeft_MouseMoveEvent(object sender, AxWMPLib._WMPOCXEvents_MouseMoveEvent e)
        {
            pictureBox_BaseLeft.BackColor = Color.Red;
        }

        private void axWMP_BaseMiddle_MouseMoveEvent(object sender, AxWMPLib._WMPOCXEvents_MouseMoveEvent e)
        {
            pictureBox_BaseMiddle.BackColor = Color.Red;
        }

        private void axWMP_BaseRight_MouseMoveEvent(object sender, AxWMPLib._WMPOCXEvents_MouseMoveEvent e)
        {
            pictureBox_BaseRight.BackColor = Color.Red;
        }

    #endregion

        //加载页面
        private void RecentVideoForm_Load(object sender, EventArgs e)
        {
            try
            {
                strExePath = System.AppDomain.CurrentDomain.BaseDirectory;    //获取程序当前文件夹地址
                strExePath += @"RecentVideos.txt";  //找到保存最近图片信息的文件

                //从最近使用图片文件中读取图片的使用信息
                StreamReader sr = new StreamReader(strExePath, System.Text.Encoding.Default);

                char[] splitChar = { '*' };

                Dictionary<string, int> RecentVideo = new Dictionary<string, int>();

                string sLine = "";
                sLine = sr.ReadLine();
                if (sLine != null)
                {
                    //string[] strSplit = sLine.Split(new char[] { '&' });
                    string[] strSplit = sLine.Split(splitChar);
                    //添加进词典
                    RecentVideo.Add(strSplit[0].ToString(), int.Parse(strSplit[1].ToString()));

                    while (sLine != null)
                    {
                        sLine = sr.ReadLine();
                        if (sLine != null)
                        {
                            //strSplit = sLine.Split(new char[] { '&' });
                            strSplit = sLine.Split(splitChar);
                            //添加进词典
                            RecentVideo.Add(strSplit[0].ToString(), int.Parse(strSplit[1].ToString()));
                        }
                    }
                }

                sr.Close();

                string[] video = new string[RecentVideo.Count];
                int[] fre = new int[RecentVideo.Count];

                //复制并排序
                RecentVideo.Values.CopyTo(fre, 0);
                RecentVideo.Keys.CopyTo(video, 0);
                System.Array.Sort(fre, video);
                //System.Array.Reverse(fre);
                //System.Array.Reverse(video);

                count = 0;  //计数器清0

                //显示
                #region
                for (int i = RecentVideo.Count - 1; i >= 0; i--)
                {
                    if (count > 5)
                        break;

                    if (fre[i] != 0 && count < 5)
                    {
                        if (count == 0)
                        {
                            //在top_left控件中显示视频图片
                            axWMP_TopLeft.settings.mute = true;
                            axWMP_TopLeft.URL = video[i];
                            //axWMP_TopLeft.Ctlcontrols.play();
                            //timer_control.Enabled = true;

                            label_TopLeft.Text = Path.GetFileName(video[i]);
                            count++;
                        }
                        else
                        {
                            if (count == 1)
                            {
                                //在top_middle控件中显示图片
                                axWMP_TopMiddle.settings.mute = true;
                                axWMP_TopMiddle.URL = video[i];
                                //axWMP_TopMiddle.Ctlcontrols.play();
                                //timer_control.Enabled = true;

                                label_TopMiddle.Text = Path.GetFileName(video[i]);
                                count++;
                            }
                            else
                            {
                                if (count == 2)
                                {
                                    //在top_right控件中显示图片
                                    axWMP_TopRight.settings.mute = true;
                                    axWMP_TopRight.URL = video[i];
                                    //axWMP_TopRight.Ctlcontrols.play();
                                    //timer_control.Enabled = true;

                                    label_TopRight.Text = Path.GetFileName(video[i]);
                                    count++;
                                }
                                else
                                {
                                    if (count == 3)
                                    {
                                        //在base_left控件中显示图片
                                        axWMP_BaseLeft.URL = video[i];
                                        //axWMP_BaseLeft.Ctlcontrols.play();
                                        //timer_control.Enabled = true;

                                        label_BaseLeft.Text = Path.GetFileName(video[i]);
                                        count++;
                                    }
                                    else
                                    {
                                        if (count == 4)
                                        {
                                            //在base_middle控件中显示图片
                                            axWMP_BaseMiddle.URL = video[i];
                                            //axWMP_BaseMiddle.Ctlcontrols.play();
                                            //timer_control.Enabled = true;

                                            label_BaseMiddle.Text = Path.GetFileName(video[i]);
                                            count++;
                                        }
                                        else
                                        {
                                            if (count == 5)
                                            {
                                                //在base_right控件中显示图片
                                                axWMP_BaseRight.URL = video[i];
                                                //axWMP_BaseRight.Ctlcontrols.play();
                                                //timer_control.Enabled = true;

                                                label_BaseRight.Text = Path.GetFileName(video[i]);
                                                count++;
                                            }
                                        }
                                    }
                                }
                            }
                        }

                    }
                }//end for
                #endregion

            }
            catch
            {
                MessageBox.Show("无法打开视频文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //单击“更多视频”
        private void label_moreVideo_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openVideo = new OpenFileDialog();
                openVideo.FileName = "";
                openVideo.Filter = "avi格式(*.avi)|*.avi|所有文件(*.*)|*.*";

                if (openVideo.ShowDialog() == DialogResult.OK)
                {
                    //修改频率
                    ModifyFre mf = new ModifyFre();
                    mf.ModifyFreVideo(openVideo.FileName);

                    VideoForm vf = new VideoForm();
                    //vf.Owner = this;
                    vf.MdiParent = this;
                    vf.WindowState = FormWindowState.Maximized;
                    vf.axWindowsMediaPlayer_main.URL = openVideo.FileName;
                    //vf.axWindowsMediaPlayer_main.settings.autoStart = true;   //不自动播放
                    vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                    vf.Show();
                }
            }
            catch
            {
                MessageBox.Show("打开指定文件失败!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        //计时器控制
        private void timer_control_Tick(object sender, EventArgs e)
        {
            try
            {
                if (temp >= 1)
                {
                    //静音
                    axWMP_TopLeft.settings.mute = true;
                    axWMP_TopMiddle.settings.mute = true;
                    axWMP_TopRight.settings.mute = true;
                    axWMP_BaseLeft.settings.mute = true;
                    axWMP_BaseMiddle.settings.mute = true;
                    axWMP_BaseRight.settings.mute = true;

                    timer_control.Enabled = false;
                }
                else
                    temp++;
            }
            catch { }

        }

        //单击top_left标签
        private void label_TopLeft_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            VideoForm vf = new VideoForm();
            vf.MdiParent = mf;
            vf.WindowState = FormWindowState.Maximized;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFreVideo(this.axWMP_TopLeft.URL);

                vf.axWindowsMediaPlayer_main.URL = this.axWMP_TopLeft.URL;
                vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                vf.Show();

                this.Close();   //关闭最近使用视频窗口
            }
            catch
            {
                vf.Close();
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击top_middle标签
        private void label_TopMiddle_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            VideoForm vf = new VideoForm();
            vf.MdiParent = mf;
            vf.WindowState = FormWindowState.Maximized;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFreVideo(this.axWMP_TopMiddle.URL);

                vf.axWindowsMediaPlayer_main.URL = this.axWMP_TopMiddle.URL;
                vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                vf.Show();

                this.Close();   //关闭最近使用视频窗口
            }
            catch
            {
                vf.Close();
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击top_right标签
        private void label_TopRight_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            VideoForm vf = new VideoForm();
            vf.MdiParent = mf;
            vf.WindowState = FormWindowState.Maximized;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFreVideo(this.axWMP_TopRight.URL);

                vf.axWindowsMediaPlayer_main.URL = this.axWMP_TopRight.URL;
                vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                vf.Show();

                this.Close();   //关闭最近使用视频窗口
            }
            catch
            {
                vf.Close();
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击base_left标签
        private void label_BaseLeft_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            VideoForm vf = new VideoForm();
            vf.MdiParent = mf;
            vf.WindowState = FormWindowState.Maximized;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFreVideo(this.axWMP_BaseLeft.URL);

                vf.axWindowsMediaPlayer_main.URL = this.axWMP_BaseLeft.URL;
                vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                vf.Show();

                this.Close();   //关闭最近使用视频窗口
            }
            catch
            {
                vf.Close();
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击base_middle标签
        private void label_BaseMiddle_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            VideoForm vf = new VideoForm();
            vf.MdiParent = mf;
            vf.WindowState = FormWindowState.Maximized;

            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFreVideo(this.axWMP_BaseMiddle.URL);

                vf.axWindowsMediaPlayer_main.URL = this.axWMP_BaseMiddle.URL;
                vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                vf.Show();

                this.Close();   //关闭最近使用视频窗口
            }
            catch
            {
                vf.Close();
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击base_right标签
        private void label_BaseRight_Click(object sender, EventArgs e)
        {
            MainForm mf = new MainForm();
            mf = (MainForm)this.Owner;

            VideoForm vf = new VideoForm();
            vf.MdiParent = mf;
            vf.WindowState = FormWindowState.Maximized;
            
            try
            {
                //修改频率
                ModifyFre mff = new ModifyFre();
                mff.ModifyFreVideo(this.axWMP_BaseRight.URL);

                vf.axWindowsMediaPlayer_main.URL = this.axWMP_BaseRight.URL;
                vf.label_main.Text = "名称: " + vf.axWindowsMediaPlayer_main.currentMedia.name + "\r\n" + "\r\n" + "类型: " + vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileType") + "\r\n" + "\r\n" + "大小: " + (long.Parse(vf.axWindowsMediaPlayer_main.currentMedia.getItemInfo("FileSize")) / 1024 / 1024).ToString() + "MB\r\n" + "\r\n" + "持续时间: "; //+ vf.axWindowsMediaPlayer_main.currentMedia.durationString;
                vf.Show();

                this.Close();   //关闭最近使用视频窗口
            }
            catch
            {
                vf.Close();
                MessageBox.Show("打开指定文件失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        //单击top_left删除
        //private void btn_TopLeft_del_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        strExePath = System.AppDomain.CurrentDomain.BaseDirectory;    //获取程序当前文件夹地址
        //        strExePath += @"RecentVideos.txt";  //找到保存最近图片信息的文件

        //        //从最近使用图片文件中读取图片的使用信息
        //        StreamReader sr = new StreamReader(strExePath, System.Text.Encoding.Default);

        //        char[] splitChar = { '*' };

        //        string sLine = "";
        //        sLine = sr.ReadLine();
        //        string[] strSplit = sLine.Split(splitChar);
        //        sLine = strSplit[0].ToString();

        //        if (sLine != this.axWMP_TopLeft.URL)
        //        {
        //            while (sLine != null)
        //            {
        //                sLine = sr.ReadLine();
        //                if (sLine != null)
        //                {
        //                    //strSplit = sLine.Split(new char[] { '&' });
        //                    strSplit = sLine.Split(splitChar);
        //                    //添加进词典
        //                    RecentVideo.Add(strSplit[0].ToString(), int.Parse(strSplit[1].ToString()));
        //                }
        //            }
        //        }

        //        sr.Close();

        //        this.axWMP_TopLeft.Ctlcontrols.stop();
        //        this.label_TopLeft.Text = "视频1";
        //    }
        //    catch
        //    {
        //        MessageBox.Show("删除失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        //    }
        //}
    }
}
